import portal from '~/net/portal'
import ErrorCode from '~/constant/ErrorCode'

export default function (context, next) {
    if (process.server) {
        Promise.all([
            portal.getUserInfo({ client: context.req }),
            portal.identities({ client: context.req })
        ]).then((arr) => {
            context.user = arr[0].data || null
            context.identities = arr[1].data || null
            context.store.commit('SET_USER', context.user)
            context.store.commit('SET_IDENTITY', context.identities)
            next()
        }).catch((err) => {
            if (err.code == ErrorCode.unLogin) {
                context.user = null
                context.store.commit('SET_USER', null)
                context.store.commit('SET_IDENTITY', null)
            }
            next()
        })
    } else {
        next()
    }

}